package com.vipheyue.lanjingapp.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.gson.Gson;
import com.vipheyue.lanjingapp.R;
import com.vipheyue.lanjingapp.activity.WebActivity;
import com.vipheyue.lanjingapp.bean.ReceiverBean;

import cn.bmob.push.PushConstants;

public class MyPushMessageReceiver extends BroadcastReceiver {
    public MyPushMessageReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        // TODO Auto-generated method stub
        try {
            if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
                String receiverinfo = intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING);
                Log.d("bmob", "客户端收到推送内容：" + receiverinfo);
                ReceiverBean symPushBean = new Gson().fromJson(receiverinfo, ReceiverBean.class);
                NotificationCompat.Builder mBuilder =
                        new NotificationCompat.Builder(context)
                                .setSmallIcon(R.mipmap.ic_launcher)
                                .setContentTitle(context.getString(R.string.app_name))
                                .setContentText(symPushBean.getAlert())
                                .setDefaults(Notification.DEFAULT_SOUND);
                Intent resultIntent = new Intent(context, WebActivity.class);
//                resultIntent.putExtra("url", symPushBean.getUrl());
                PendingIntent resultPendingIntent =
                        PendingIntent.getActivity(
                                context,
                                0,
                                resultIntent,
                                PendingIntent.FLAG_UPDATE_CURRENT
                        );
                mBuilder.setContentIntent(resultPendingIntent);
                int mNotificationId = 001;
                NotificationManager mNotifyMgr =
                        (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
                Notification notification = mBuilder.build();
                notification.flags = Notification.FLAG_AUTO_CANCEL;
                mNotifyMgr.notify(mNotificationId, notification);
                Log.d("bmob", "客户端收到推送内容：......." );
            }
        } catch (Exception e) {

        }

    }
}
